<?php
if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class tools_module_table{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/*
	*	取出一条信息
	*/
	public function one_info($tmt_id='') {
		$info = array();
		if($tmt_id){
			$sql = "SELECT * FROM ".DB::table('tools_module_table')." WHERE tmt_id='".$tmt_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}
    
	/*
	*  添加系统信息
	*/
	public function add() {
        global $_G;
        //DEBUG 接收并获取数据表所属模块名称 开始
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tm_name = '请选择模块';
        $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
        if(file_exists($tools_module_class_path) && $tm_id){
			require_once $tools_module_class_path;
			$tools_module = new tools_module();
            $tools_module_info = $tools_module->one_info($tm_id);
            $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
		}
        //DEBUG 接收并获取数据表所属模块名称 结束
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$tools_module_table = isset($_REQUEST['tools_module_table']) && !empty($_REQUEST['tools_module_table']) ? $_REQUEST['tools_module_table']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验
            $tools_module_table['create_dateline'] = TIMESTAMP;
            $insert_id = DB::insert('tools_module_table',$tools_module_table,true);
            if($insert_id){
                showmessage('操作成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }else{
                showmessage('操作失败或信息未发生变更','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
		}else{
			include template('tools/module_table/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$tools_module_table = isset($_REQUEST['tools_module_table']) && !empty($_REQUEST['tools_module_table']) ? $_REQUEST['tools_module_table']:'';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tmt_id){
            if('1'==$issubmit){
                $tools_module_table['modify_dateline'] = TIMESTAMP;
                $where = array('tmt_id'=>$tmt_id);
                $effect_row = DB::update('tools_module_table',$tools_module_table,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tools_module_table['tm_id']);
                }else{
                    showmessage('操作失败','index.php?mod=tools&action=module_table&do=index&tm_id='.$tools_module_table['tm_id']);
                }
            }else{
                //DEBUG 获取节点信息
                $info = self::one_info($tmt_id);
                //DEBUG 接收并获取数据表所属模块名称 开始
                $tm_id = $info['tm_id'];
                $tm_name = '请选择模块';
                $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
                if(file_exists($tools_module_class_path) && $tm_id){
                    require_once $tools_module_class_path;
                    $tools_module = new tools_module();
                    $tools_module_info = $tools_module->one_info($tm_id);
                    $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
                }
                //DEBUG 接收并获取数据表所属模块名称 结束
                include template('tools/module_table/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=tools&action=module_table&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function delete() {
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tmt_id){
            DB::delete("tools_module_table", array("tmt_id" => $tmt_id), $limit=1);
            //TODO 逻辑删需要创建isdelete字段 0=未删除 1=已删除
            //DB::update("tools_module_table",array('isdelete'=>1), array("tmt_id" => $tmt_id), $limit=1);
            showmessage('操作成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
        }
    }
	/*
	*  系统信息列表
	*/
	public function index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND tmt_table_name LIKE '%".$keyword."%'";
        }
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        if($tm_id){
            $wheresql .= " AND tm_id = '".$tm_id."' ";
        }
        $sql_info = "SELECT * FROM ".DB::table('tools_module_table')." WHERE 1=1 ".$wheresql." ORDER BY tmt_id DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
        if(file_exists($tools_module_class_path)){
			require_once $tools_module_class_path;
			$tools_module = new tools_module();
		}
        foreach($sql_info_result AS $key => $value){
            $tools_module_info = $tools_module->one_info($value['tm_id']);
            if($tools_module_info){
                $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
            }else{
                $tm_name = '请选择模块';
            }
            $sql_info_result[$key]['tm_name']=$tm_name;
        }
        $sql_total_rows = "SELECT count(*) FROM ".DB::table('tools_module_table')." WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        $multipage = multi($sql_total_rows_result, $perpage, $page, "index.php?mod=tools&action=module_table&do=index&tm_id=".$tm_id);
		include template('tools/module_table/index');	
	}
    /**
     * 根据数据库表设置 自动获取模块数据表结构
     * 并插入获取到的数据库表字段到系统模块表字段数据表 tools_module_field 中
     *
     * @param $tmt_id int 模块表主键
     *
     * @return 跳转提示
     */
    public function auto_build_database(){
        global $_G;
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tm_id && $tmt_id){
            //DEBUG 获取tools_module_field数据表内所有属于操作对象的字段并拼接SQL 开始
            $sql = "SELECT * FROM ".DB::table('tools_module_table')." WHERE tmt_id='".$tmt_id."' LIMIT 1";
            $tools_module_table = DB::fetch_first($sql);
            $sql = "SHOW FULL COLUMNS FROM ".DB::table($tools_module_table['tmt_table_name'])."";
            $tools_module_table_fields = DB::fetch_all($sql);
            //DEBUG 获取到的数据库表结构
            /*
             * 一个示例
             *  [Field] => cc_id
             *  [Type] => bigint(20)
             *  [Collation] => 
             *  [Null] => NO
             *  [Key] => PRI
             *  [Default] => 
             *  [Extra] => auto_increment
             *  [Privileges] => select,insert,update,references
             *  [Comment] => 优惠券编号
             * 
             * 字段说明
             * Field 字段名称
             * Type 字段类型
             * Collation 
             * Null 是否允许空
             * Key 索引类型 （PRI 主键约束, UNI 唯一约束, MUL 可以重复）
             * Default 默认值
             * 
             */
            $i=0;
            foreach($tools_module_table_fields AS $key => $value){
                $tmf_filed_index=$tmf_filed_key=0;
                $tmf_type='input';
                $tmf_type_value=1;
                if($value['Key']=='PRI' && $value['Extra']=='auto_increment'){
                    $tmf_filed_key=1;
                    $tmf_type='';
                }elseif($value['Key']=='PRI' || $value['Key']=='UNI' || $value['Key']=='MUL'){
                    $tmf_filed_index=1;
                }
                $tmf_field_type = 'input';//TODO 各种形式自动识别默认字符
                $tmf_field_length = 1;
                $Type = trim($value['Type'],')');
                $Type_array = explode('(',$Type);
                if($Type_array[0]){
                    $tmf_field_type = strtoupper($Type_array[0]);
                }
                if($Type_array[1]){
                    $tmf_field_length=$Type_array[1];
                }
                $tools_module_field=array(
                    'tm_id'=>$tm_id,
                    'tmt_id'=>$tmt_id,
                    'tmf_field_name'=>$value['Field'],
                    'tmf_filed_key'=>$tmf_filed_key,
                    'tmf_filed_search'=>0,
                    'tmf_filed_index'=>$tmf_filed_index,
                    'tmf_field_type'=>$tmf_field_type,
                    'tmf_field_length'=>$tmf_field_length,
                    'tmf_field_default'=>$value['Default'],
                    'tmf_field_list'=>1,
                    'tmf_name'=>$value['Comment'],
                    'tmf_type'=>$tmf_type,
                    'tmf_type_value'=>'',
                    'tmf_description'=>$value['Comment'],
                    'create_dateline'=>TIMESTAMP       
                );
                $sys_fields = array('isdelete','modify_dateline','create_dateline');
                if(in_array($value['Field'],$sys_fields)){
                    $i++;
                }else{
                    $insert_id = DB::insert('tools_module_field',$tools_module_field,true);
                    if($insert_id){
                        $i++;
                    }   
                }
            }
            $check_tools_moudle_table_result=0;
            if($i==count($tools_module_table_fields)){
                $check_tools_moudle_table_result=1;//TODO 校验数据库获取是否完整
            }
            if(!empty($check_tools_moudle_table_result)){
                showmessage('获取表字段对象成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
            }else{
                showmessage('获取表字段对象失败','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
            }
        }else{
            showmessage('请选择表对象操作','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
        }
    }
    /**
     * 根据数据库设置 生成模块数据表数据结构
     *
     * @param $tmt_id int 模块表主键
     *
     * @return 跳转提示
     */
    public function build_database(){
        global $_G;
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tm_id && $tmt_id){
            //DEBUG 获取tools_module_field数据表内所有属于操作对象的字段并拼接SQL 开始
            $sql = "SELECT * FROM ".DB::table('tools_module_table')." WHERE tmt_id='".$tmt_id."' LIMIT 1";
            $tools_module_table = DB::fetch_first($sql);
            $sql = "SELECT * FROM ".DB::table('tools_module_field')." WHERE tmt_id='".$tmt_id."' ORDER BY tmf_id ASC";
            $tools_module_field = DB::fetch_all($sql);
            $tools_crud_add_key = $tools_crud_add_keyword = array();
            foreach($tools_module_field AS $key => $value){
                //DEBUG 获取主键
                if(1==$value['tmf_filed_key']){
                    $tools_crud_add_key = $value;
                    unset($tools_module_field[$key]);
                }
                //DEBUG 获取主关键字搜索字段
                if(1==$value['tmf_filed_search']){
                    $tools_crud_add_keyword = $value;
                }else{
                     //DEBUG 获取设置加索引字段
                    if(1==$value['tmf_filed_index']){
                        $tools_crud_add_index[$value['tmf_field_name']] = $value['tmf_field_name'];
                    }
                }
            }
            //DEBUG 获取tools_module_field数据表内所有属于操作对象的字段并拼接SQL 结束
            //DEBUG 执行拼装SQL
            $runsql = "DROP TABLE IF EXISTS `pre_".$tools_module_table['tmt_table_name']."`;
            CREATE TABLE `pre_".$tools_module_table['tmt_table_name']."` (";
            if($tools_crud_add_key){
               $runsql .= "`".$tools_crud_add_key['tmf_field_name']."` ".$tools_crud_add_key['tmf_field_type']."(".$tools_crud_add_key['tmf_field_length'].") NOT NULL AUTO_INCREMENT,";
            }
            foreach($tools_module_field AS $key => $value){
                $runsql .= "`".$value['tmf_field_name']."` ".$value['tmf_field_type']."";
                if($value['tmf_field_length']){
                    $runsql .= "(".$value['tmf_field_length'].") ";
                }
                if(isset($value['tmf_field_default'])){
                    if('INT'==$value['tmf_field_type'] && empty($value['tmf_field_default'])){
                        $runsql .= " DEFAULT 0";
                    }else{
                        $runsql .= " DEFAULT '".$value['tmf_field_default']."'";
                    }
                }
                if($value['tmf_name']){
                    $runsql .= " COMMENT '".$value['tmf_name']."'";
                }
                $runsql .= ",";
            }
            //DEBUG 附加基本字段
            $runsql .= "
            `create_dateline` int(10) DEFAULT '0' COMMENT '创建时间',
            `modify_dateline` int(10) DEFAULT '0' COMMENT '修改时间',
            `isdelete` tinyint(1) unsigned zerofill DEFAULT '0',";
            if($tools_crud_add_key){
               $runsql .= "PRIMARY KEY (`".$tools_crud_add_key['tmf_field_name']."`),";
            }
            if($tools_crud_add_keyword){
               $runsql .= "KEY `".$tools_crud_add_keyword['tmf_field_name']."` (`".$tools_crud_add_keyword['tmf_field_name']."`),";
            }
            foreach($tools_crud_add_index AS $key => $value){
               $runsql .= "KEY `".$value."` (`".$value."`),";
            }
            $runsql = trim($runsql,',');
            $runsql .= ") ENGINE=MyISAM COMMENT='".$tools_module_table['tmt_name']."' DEFAULT CHARSET=utf8;";
            
            runquery($runsql);
            //DEBUG 判断数据表是否生成成功
            $check_tools_moudle_table = "SHOW TABLES LIKE '%".DB::table($tools_module_table['tmt_table_name'])."%';";
            $check_tools_moudle_table_result = DB::fetch_first($check_tools_moudle_table);
            if(!empty($check_tools_moudle_table_result)){
                showmessage('生成对象成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
            }else{
                showmessage('生成对象失败','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
            }
        }else{
            showmessage('请选择生成对象','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id.'&tmt_id='.$tm_id);
        }
    }
    
    /**
     * 根据模块数据库设置 生成模块程序基础结构
     *
     * @param $tmt_id int 模块表主键
     *
     * @return 跳转提示
     */
    public function generating_program(){
        global $_G;
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tm_id && $tmt_id){
            //DEBUG 获取模块 数据表 及 表内字段对象 开始
            $sql = "SELECT * FROM ".DB::table('tools_module')." WHERE tm_id='".$tm_id."' LIMIT 1";
            $tools_module = DB::fetch_first($sql);
            $sql = "SELECT * FROM ".DB::table('tools_module_table')." WHERE tmt_id='".$tmt_id."' LIMIT 1";
            $tools_module_table = DB::fetch_first($sql);
            $sql = "SELECT * FROM ".DB::table('tools_module_field')." WHERE tmt_id='".$tmt_id."' ORDER BY tmf_id ASC";
            $tools_module_field = DB::fetch_all($sql);
            $tools_crud_field_name_array = $tools_crud_field_array = $tools_crud_add_key = $tools_crud_add_keyword = array();
            foreach($tools_module_field AS $key => $value){
                //DEBUG 获取主键
                if(1==$value['tmf_filed_key']){
                    $tools_crud_add_key = $value;
                    unset($tools_module_field[$key]);
                }elseif(1==$value['tmf_filed_search']){
                    //DEBUG 获取主关键字搜索字段
                    $tools_crud_add_keyword = $value;
                    $tools_crud_field_array[] = $value['tmf_field_name'];
                    $tools_crud_field_name_array[] = $value['tmf_name'];
                    $tools_crud_field_tmf_type_array[] = $value['tmf_type'];
                    $tools_crud_field_tmf_type_value_array[] = $value['tmf_type_value'];
                }else{
                    $tools_crud_field_array[] = $value['tmf_field_name'];
                    $tools_crud_field_name_array[] = $value['tmf_name'];
                    $tools_crud_field_tmf_type_array[] = $value['tmf_type'];
                    $tools_crud_field_tmf_type_value_array[] = $value['tmf_type_value'];
                }
                //DEBUG 增加是否列表显示功能数组 
                $tools_crud_field_list_array[$value['tmf_field_name']] = $value['tmf_field_list'];
                $tools_crud_field_name_list_array[$value['tmf_name']] = $value['tmf_field_list'];
            }
            //DEBUG 获取模块 数据表 及 表内字段对象 结束
            //DEBUG 根据模块表字段参数生成基础CRUD程序 开始
            $tools_crud_add = array(
                'menu_pid'=>$tools_module['tm_menu_pid'],
                'name_var'=>$tools_module_table['tmt_name'],
                'mod'=>$tools_module['tm_name'],
                'action'=>str_replace($tools_module['tm_name'].'_', '', $tools_module_table['tmt_table_name']),
                'key'=>$tools_crud_add_key['tmf_field_name'],
                'keyword'=>$tools_crud_add_keyword['tmf_field_name'],
                'field_array'=>$tools_crud_field_array,
                'field_name_array'=>$tools_crud_field_name_array,
                'tmf_type_array'=>$tools_crud_field_tmf_type_array,
                'tmf_type_value_array'=>$tools_crud_field_tmf_type_value_array
            );
            $tools_crud_position = $tools_module['tm_position'];
            $tools_crud_position_dir = '';
            $tools_crud_position_pre = '';
            $tools_crud_setting_tpldir = $_G['setting']['tpldir'];
            if(1==$tools_crud_position){
               $tools_crud_position_dir=$_G['config']['admin_folder'].'/';
               $tools_crud_position_pre=$_G['config']['admin_folder'];
               $tools_crud_setting_tpldir = $_G['setting']['admin_tpldir'];
            }
            //DEBUG 数据空校验
            foreach($tools_crud_add AS $key => $value){
               if(empty($value) && 'keyword' != $key){
                   showmessage('操作失败,请正确填写信息','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
               }
            }
            //DEBUG 字段与字段名称 数量是否匹配
            $tools_crud_add_count=count($tools_crud_add['field_array'])+2;
            //DEBUG 判断数据表是否存在
            $table_name = $tools_crud_add['mod'].'_'.$tools_crud_add['action'];
            $sql = "SHOW TABLES LIKE '%". DB::table($table_name)."%'";
            $result = DB::fetch_first($sql);
            if(empty($result)){
                showmessage('数据表名称不存在,请先运行创建数据表','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }else{
                //TODO 创建数据库 暂无字段类型的预定义与索引外键的界面处理,隐藏数据库暂由人工创建
                //showmessage('数据表名称已存在,请修改mod或action','index.php?mod=tools&action=crud&do=add');
                //DEBUG 生成模块的相关目录文件
                //DEBUG 创建class目录与文件
                $class_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'source/class/'.$tools_crud_add['mod'];
                $class_file_path = $class_dir_path.'/'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'.php';
                if(file_exists($class_file_path)){
                    showmessage($class_file_path.'文件已存在,请检查','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
                }else{
                    dmkdir($class_dir_path);
                    $src_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/source_class.php';
                    $target_file = $class_file_path;
                    $this->upload_file($src_file,$target_file);
                    //DEBUG 替换关键字标签
                    $target_file_content = @file_get_contents($target_file);
                    $target_file_content_new = str_replace(array('###tools_crud_add_mod###','###tools_crud_add_action###','###tools_crud_add_key###','###tools_crud_add_keyword###'),array($tools_crud_add['mod'],$tools_crud_add['action'],$tools_crud_add['key'],$tools_crud_add['keyword']),$target_file_content);
                    @file_put_contents($target_file,$target_file_content_new);
                }
                //DEBUG 创建module目录与文件
                $module_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'source/module/'.$tools_crud_add['mod'];
                $module_file_path = $module_dir_path.'/'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'.php';
                if(file_exists($module_file_path)){
                    showmessage($module_file_path.'文件已存在,请检查','index.php?mod=tools&action=crud&do=add');
                }else{
                    dmkdir($module_dir_path);
                    $src_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/source_module.php';
                    $target_file = $module_file_path;
                    //DEBUG 替换关键字标签
                    $target_file_content = @file_get_contents($src_file);
                    $target_file_content_new = str_replace(array('###tools_crud_add_mod###','###tools_crud_add_action###','###tools_crud_add_key###','###tools_crud_add_keyword###'),array($tools_crud_add['mod'],$tools_crud_add['action'],$tools_crud_add['key'],$tools_crud_add['keyword']),$target_file_content);
                    @file_put_contents($target_file,$target_file_content_new);
                }
                //DEBUG 创建template目录与文件
                $template_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'template/'.$tools_crud_setting_tpldir.'/'.$tools_crud_add['mod'].'/'.$tools_crud_add['action'];
                $template_file_add_path = $template_dir_path.'/'.'add.htm';
                if(is_dir($template_dir_path)){
                    showmessage($template_dir_path.'目录已存在,请检查','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
                }else{
                    dmkdir($template_dir_path);
                }
                if(file_exists($template_file_add_path)){
                    showmessage($template_file_add_path.'文件已存在,请检查','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
                }else{
                    //DEBUG 设置自定义显示字段 ###add_field###
                    $add_field = '';
                    $tools_crud_add_add_field='';
                    if(!empty($tools_crud_add['field_array']) && !empty($tools_crud_add['field_name_array'])){
                        $air_textarea = $air_textarea_count= 0;
                        foreach($tools_crud_add['field_array'] AS $key => $value){
                            if('air_textarea'==$tools_crud_add['tmf_type_array'][$key] && empty($air_textarea_count)){
                                if(empty($tools_crud_position_dir)){
                                    $plugins_path = './';
                                }else{
                                    $plugins_path = '../';
                                }
                                $tools_crud_add_add_field .='<!--百度UEditor编辑器引入 开始-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor.config.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor.all.js"> </script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor_dzf_ext.js"> </script>
                                    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
                                    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/lang/zh-cn/zh-cn.js"></script>
                                    <!--增加公式插件开始-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/addKityFormulaDialog.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/getKfContent.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/defaultFilterFix.js"></script>
                                    <!--百度UEditor编辑器引入 结束-->';
                                $air_textarea_count++;
                            }elseif('select'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_option = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_option .= '<option value="'.$tmp[0].'">'.$tmp[1].'</option>';
                                    }
                                }
                            }elseif('radio'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_radio = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_radio .= '
                                        <div class="radio" style="float:left">
                                            <label>
                                                <input value="'.$tmp[0].'" <!--{if '.$tmp[0].'==$info[###tools_crud_add_field###]}-->checked="checked"<!--{/if}--> id="###tools_crud_add_mod###_###tools_crud_add_action###_###tools_crud_add_field###"_0" name="###tools_crud_add_mod###_###tools_crud_add_action###[###tools_crud_add_field###]" type="radio" class="ace input-lg" />
                                                <span class="lbl bigger-120"> '.$tmp[1].'</span>
                                            </label>
                                        </div>';
                                    }
                                }
                            }elseif('checkbox'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_checkbox = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_checkbox .= '
                                        <div class="checkbox" style="float:left">
                                            <label class="block">
                                                <input value="'.$tmp[0].'" <!--{if '.$tmp[0].'==$info[###tools_crud_add_field###]}-->checked="checked"<!--{/if}--> id="###tools_crud_add_mod###_###tools_crud_add_action###_###tools_crud_add_field###"_'.$k.'" name="###tools_crud_add_mod###_###tools_crud_add_action###[###tools_crud_add_field###][]" type="checkbox" class="ace input-lg" />
                                                <span class="lbl bigger-120"> '.$tmp[1].'</span>
                                            </label>
                                        </div>';
                                    }
                                }
                            }
                            $src_add_field_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/add_field_'.strtolower($tools_crud_add['tmf_type_array'][$key]).'.htm';
                            $src_add_field_content = @file_get_contents($src_add_field_file);                            
                            $tools_crud_add_add_field .= str_replace(array('###tools_crud_add_field_checkbox###','###tools_crud_add_field_radio###','###tools_crud_add_field_option###','###tools_crud_add_field###','###tools_crud_add_field_name###'),array($filed_select_checkbox,$filed_select_radio,$filed_select_option,$tools_crud_add['field_array'][$key],$tools_crud_add['field_name_array'][$key]),$src_add_field_content).PHP_EOL;
                        }
                    }
                    $src_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/add.htm';
                    $target_file = $template_file_add_path;
                    $this->upload_file($src_file,$target_file);
                    $target_file_content = @file_get_contents($target_file);
                    //DEBUG 替换自定义字段
                    $target_file_content_tmp = str_replace('###add_field###',$tools_crud_add_add_field,$target_file_content);
                    $target_file_content_new = str_replace(array('###tools_crud_add_name_var###','###tools_crud_add_mod###','###tools_crud_add_action###','###tools_crud_add_key###'),array($tools_crud_add['name_var'],$tools_crud_add['mod'],$tools_crud_add['action'],$tools_crud_add['key']),$target_file_content_tmp);
                    @file_put_contents($target_file,$target_file_content_new);
                }
                $template_file_edit_path = $template_dir_path.'/'.'edit.htm';
                if(file_exists($template_file_edit_path)){
                    showmessage($template_file_edit_path.'文件已存在,请检查','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
                }else{
                    //DEBUG 设置自定义显示字段 ###add_field###
                    $add_field = '';
                    $tools_crud_add_edit_field='';
                    if(!empty($tools_crud_add['field_array']) && !empty($tools_crud_add['field_name_array'])){
                        $air_textarea = $air_textarea_count= 0;
                        foreach($tools_crud_add['field_array'] AS $key => $value){
                            if('air_textarea'==$tools_crud_add['tmf_type_array'][$key] && empty($air_textarea_count)){
                                if(empty($tools_crud_position_dir)){
                                    $plugins_path = './';
                                }else{
                                    $plugins_path = '../';
                                }
                                $tools_crud_add_edit_field .='<!--百度UEditor编辑器引入 开始-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor.config.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor.all.js"> </script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/ueditor_dzf_ext.js"> </script>
                                    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
                                    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/lang/zh-cn/zh-cn.js"></script>
                                    <!--增加公式插件开始-->
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/addKityFormulaDialog.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/getKfContent.js"></script>
                                    <script type="text/javascript" charset="utf-8" src="'.$plugins_path.'plugins/editors/ueditor/kityformula-plugin/defaultFilterFix.js"></script>
                                    <!--百度UEditor编辑器引入 结束-->';
                                $air_textarea_count++;
                            }elseif('select'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_option = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_option .= '<option <!--{if '.$tmp[0].'==$info[###tools_crud_add_field###]}-->selected<!--{/if}--> value="'.$tmp[0].'">'.$tmp[1].'</option>';
                                    }
                                }
                            }elseif('radio'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_radio = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_radio .= '
                                        <div class="radio" style="float:left">
                                            <label>
                                                <input value="'.$tmp[0].'" <!--{if '.$tmp[0].'==$info[###tools_crud_add_field###]}-->checked="checked"<!--{/if}--> id="###tools_crud_add_mod###_###tools_crud_add_action###_###tools_crud_add_field###"_'.$k.'" name="###tools_crud_add_mod###_###tools_crud_add_action###[###tools_crud_add_field###]" type="radio" class="ace input-lg" />
                                                <span class="lbl bigger-120"> '.$tmp[1].'</span>
                                            </label>
                                        </div>';
                                    }
                                }
                            }elseif('checkbox'==$tools_crud_add['tmf_type_array'][$key]){
                                $filed_select_checkbox = '';
                                $tmf_type_value = explode(PHP_EOL,trim($tools_crud_add['tmf_type_value_array'][$key]));
                                foreach($tmf_type_value AS $k => $v){
                                    if($v){
                                        $tmp = explode('|',$v);
                                        $filed_select_checkbox .= '
                                        <div class="checkbox" style="float:left">
                                            <label class="block">
                                                <input value="'.$tmp[0].'" <!--{if in_array('.$tmp[0].',explode(",",$info["###tools_crud_add_field###"]))}-->checked="checked"<!--{/if}--> id="###tools_crud_add_mod###_###tools_crud_add_action###_###tools_crud_add_field###"_'.$k.'" name="###tools_crud_add_mod###_###tools_crud_add_action###[###tools_crud_add_field###][]" type="checkbox" class="ace input-lg" />
                                                <span class="lbl bigger-120"> '.$tmp[1].'</span>
                                            </label>
                                        </div>';
                                    }
                                }
                            }
                            $src_edit_field_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/edit_field_'.strtolower($tools_crud_add['tmf_type_array'][$key]).'.htm';
                            $src_edit_field_content = @file_get_contents($src_edit_field_file);
                            $tools_crud_add_edit_field .= str_replace(array('###tools_crud_add_field_checkbox###','###tools_crud_add_field_radio###','###tools_crud_add_field_option###','###tools_crud_add_field###','###tools_crud_add_field_name###'),array($filed_select_checkbox,$filed_select_radio,$filed_select_option,$tools_crud_add['field_array'][$key],$tools_crud_add['field_name_array'][$key]),$src_edit_field_content).PHP_EOL;
                        }
                    }
                    $src_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/edit.htm';
                    $target_file = $template_file_edit_path;
                    $this->upload_file($src_file,$target_file);
                    $target_file_content = @file_get_contents($target_file);
                    $target_file_content_tmp = str_replace('###edit_field###',$tools_crud_add_edit_field,$target_file_content);
                    $target_file_content_new = str_replace(array('###tools_crud_add_name_var###','###tools_crud_add_mod###','###tools_crud_add_action###','###tools_crud_add_key###'),array($tools_crud_add['name_var'],$tools_crud_add['mod'],$tools_crud_add['action'],$tools_crud_add['key']),$target_file_content_tmp);
                    @file_put_contents($target_file,$target_file_content_new);
                }
                $template_file_index_path = $template_dir_path.'/'.'index.htm';
                if(file_exists($template_file_index_path)){
                    showmessage($template_file_index_path.'文件已存在,请检查','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
                }else{
                    $src_file = SITE_ROOT.'./admin/source/module/tools/curd_files_'.$tools_crud_setting_tpldir.'/index.htm';
                    $target_file = $template_file_index_path;
                    $this->upload_file($src_file,$target_file);
                    $target_file_content = @file_get_contents($target_file);
                    //DEBUG 设置自定义显示字段
                    $tools_crud_add_field='';
                    if(!empty($tools_crud_add['field_array'])){
                        foreach($tools_crud_add['field_array'] AS $key => $value){
                            if(1==$tools_crud_field_list_array[$value]){
                                $tools_crud_add_field .='<td>{$value['.$value.']}</td>';
                            }
                        }
                    }
                    
                    $tools_crud_add_field_name='';
                    if(!empty($tools_crud_add['field_name_array'])){
                        foreach($tools_crud_add['field_name_array'] AS $key => $value){
                            if(1==$tools_crud_field_name_list_array[$value]){
                                $tools_crud_add_field_name .='<th>'.$value.'</th>';
                            }
                        }
                    }
                    $target_file_content_new = str_replace(array('###tools_crud_add_name_var###','###tools_crud_add_mod###','###tools_crud_add_action###','###tools_crud_add_key###','###tools_crud_add_field###','###tools_crud_add_field_name###','###tools_crud_add_count###'),array($tools_crud_add['name_var'],$tools_crud_add['mod'],$tools_crud_add['action'],$tools_crud_add['key'],$tools_crud_add_field,$tools_crud_add_field_name,$tools_crud_add_count),$target_file_content);
                    @file_put_contents($target_file,$target_file_content_new);
                }
                //DEBUG 把创建模块插入到 系统菜单表 以便系统通过角色菜单控制 把菜单分配给用户
                $common_menu_index = array(
                    'menu_pid'=>$tools_crud_add['menu_pid'],
                    'cm_mod'=>$tools_crud_add['mod'],
                    'cm_action'=>$tools_crud_add['action'],
                    'cm_do'=>'index',
                    'position'=>$tools_crud_position_pre.'_'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'_index',
                    'name_var'=>$tools_crud_add['name_var'],
                    'url'=>'index.php?mod='.$tools_crud_add['mod'].'&action='.$tools_crud_add['action'].'&do=index',
                    'create_dateline'=>TIMESTAMP
                );
                $common_menu_index_menu_id=@DB::insert('common_menu',$common_menu_index,true);
                if($common_menu_index_menu_id){
                    //DEBUG 赋予后台管理员 新建模块菜单 访问权限
                    @DB::insert('user_role_menu',array('menu_id'=>$common_menu_index_menu_id,'role_id'=>3));
                }
                //DEBUG 添加列表菜单
                $common_menu_list=$common_menu_index;
                $common_menu_list['menu_pid'] = $common_menu_index_menu_id;
                $common_menu_list['cm_do'] = 'index';
                $common_menu_list['position'] = $tools_crud_position_pre.'_'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'_'.$common_menu_list['cm_do'];
                $common_menu_list['name_var'] = $tools_crud_add['name_var'].'列表';
                $common_menu_list['url'] = 'index.php?mod='.$tools_crud_add['mod'].'&action='.$tools_crud_add['action'].'&do='.$common_menu_list['cm_do'];
                $common_menu_list_menu_id=@DB::insert('common_menu',$common_menu_list,true);
                if($common_menu_list_menu_id){
                    //DEBUG 赋予后台管理员 新建模块菜单 访问权限
                    @DB::insert('user_role_menu',array('menu_id'=>$common_menu_list_menu_id,'role_id'=>3));
                }
                //DEBUG 添加add菜单
                $common_menu_add=$common_menu_index;
                $common_menu_add['menu_pid'] = $common_menu_index_menu_id;
                $common_menu_add['cm_do'] = 'add';
                $common_menu_add['name_var'] = '添加'.$tools_crud_add['name_var'].'';
                $common_menu_add['position'] = $tools_crud_position_pre.'_'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'_'.$common_menu_add['cm_do'];
                $common_menu_add['url'] = 'index.php?mod='.$tools_crud_add['mod'].'&action='.$tools_crud_add['action'].'&do='.$common_menu_add['cm_do'];
                $common_menu_add_menu_id=@DB::insert('common_menu',$common_menu_add,true);
                if($common_menu_add_menu_id){
                    //DEBUG 赋予后台管理员 新建模块菜单 访问权限
                    @DB::insert('user_role_menu',array('menu_id'=>$common_menu_add_menu_id,'role_id'=>3));
                }
                //DEBUG 添加edit菜单
                $common_menu_edit=$common_menu_index;
                $common_menu_edit['menu_pid'] = $common_menu_index_menu_id;
                $common_menu_edit['cm_do'] = 'edit';
                $common_menu_edit['name_var'] = '编辑'.$tools_crud_add['name_var'].'';
                $common_menu_edit['position'] = $tools_crud_position_pre.'_'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'_'.$common_menu_edit['cm_do'];
                $common_menu_edit['url'] = 'index.php?mod='.$tools_crud_add['mod'].'&action='.$tools_crud_add['action'].'&do='.$common_menu_edit['cm_do'];
                $common_menu_edit['enable'] = 0;
                $common_menu_edit_menu_id=@DB::insert('common_menu',$common_menu_edit,true);
                if($common_menu_edit_menu_id){
                    //DEBUG 赋予后台管理员 新建模块菜单 访问权限
                    @DB::insert('user_role_menu',array('menu_id'=>$common_menu_edit_menu_id,'role_id'=>3));
                }
                //DEBUG 添加delete菜单
                $common_menu_delete=$common_menu_index;
                $common_menu_delete['menu_pid'] = $common_menu_index_menu_id;
                $common_menu_delete['cm_do'] = 'delete';
                $common_menu_delete['name_var'] = '删除'.$tools_crud_add['name_var'].'';
                $common_menu_delete['position'] = $tools_crud_position_pre.'_'.$tools_crud_add['mod'].'_'.$tools_crud_add['action'].'_'.$common_menu_delete['cm_do'];
                $common_menu_delete['url'] = 'index.php?mod='.$tools_crud_add['mod'].'&action='.$tools_crud_add['action'].'&do='.$common_menu_delete['cm_do'];
                $common_menu_delete['enable'] = 0;
                $common_menu_delete_menu_id=@DB::insert('common_menu',$common_menu_delete,true);
                if($common_menu_delete_menu_id){
                    //DEBUG 赋予后台管理员 新建模块菜单 访问权限
                    @DB::insert('user_role_menu',array('menu_id'=>$common_menu_delete_menu_id,'role_id'=>3));
                }
                //TODO 校验创建的模块相关文件是否存在 以便校验模块及功能是否创建成功
                updatecache('setting');
                showmessage('操作成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
        }
    }
    
    /**
     * 根据选择删除模块程序基础结构
     *
     * @param $tmt_id int 模块表主键
     *
     * @return 跳转提示
     */
    public function del_generating_program(){
        global $_G;
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tm_id){
            //DEBUG 获取模块 数据表 及 表内字段对象 开始
            $sql = "SELECT * FROM ".DB::table('tools_module')." WHERE tm_id='".$tm_id."' LIMIT 1";
            $tools_module = DB::fetch_first($sql);
            if($tmt_id){
                $sql = "SELECT * FROM ".DB::table('tools_module_table')." WHERE tmt_id='".$tmt_id."' LIMIT 1";
                $tools_module_table = DB::fetch_first($sql);
                $tools_crud_delete = array(
                    'mod'=>$tools_module['tm_name'],
                    'action'=>str_replace($tools_module['tm_name'].'_', '', $tools_module_table['tmt_table_name'])
                );
            }else{
                $tools_crud_delete = array(
                    'mod'=>$tools_module['tm_name'],
                    'action'=>''
                );
            }
        }
        $tools_crud_position = $tools_module['tm_position'];
        $tools_crud_position_dir = '';
        $tools_crud_position_pre = '';
        $tools_crud_setting_tpldir = $_G['setting']['tpldir'];
        if(1==$tools_crud_position){
           $tools_crud_position_dir=$_G['config']['admin_folder'].'/';
           $tools_crud_position_pre=$_G['config']['admin_folder'];
           $tools_crud_setting_tpldir = $_G['setting']['admin_tpldir'];
        }
        //DEBUG 删除生成模块的相关目录文件
        if(empty($tools_crud_delete['action']) && !empty($tools_crud_delete['mod'])){
            //DEBUG 删除模块目录
            $class_dir_path = realpath(SITE_ROOT.'./'.$tools_crud_position_dir.'source/class/'.$tools_crud_delete['mod']);
            $module_dir_path = realpath(SITE_ROOT.'./'.$tools_crud_position_dir.'source/module/'.$tools_crud_delete['mod']);
            $template_dir_path = realpath(SITE_ROOT.'./'.$tools_crud_position_dir.'template/'.$tools_crud_setting_tpldir.'/'.$tools_crud_delete['mod']);
            @rmkdir($class_dir_path);
            @rmkdir($module_dir_path);
            @rmkdir($template_dir_path);
            //DEBUG 判断文件是否存在 删除是否成功
            if(is_dir($class_dir_path)){
                showmessage($class_dir_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            if(is_dir($module_dir_path)){
                showmessage($module_dir_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            if(is_dir($template_dir_path)){
                showmessage($template_dir_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            //TODO 取出删除待删除的菜单编号 并删除菜单关系
            @DB::delete('common_menu',array('cm_mod'=>$tools_crud_delete['mod']));
            updatecache('setting');
            showmessage('操作成功','index.php?mod=tools&action=module&do=index');
        }elseif(!empty($tools_crud_delete['action']) && !empty($tools_crud_delete['mod'])){
            //DEBUG 删除模块功能文件与其模板目录
            $class_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'source/class/'.$tools_crud_delete['mod'];
            $module_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'source/module/'.$tools_crud_delete['mod'];
            $class_file_path = $class_dir_path.'/'.$tools_crud_delete['mod'].'_'.$tools_crud_delete['action'].'.php';
            $module_file_path = $module_dir_path.'/'.$tools_crud_delete['mod'].'_'.$tools_crud_delete['action'].'.php';
            $template_dir_path = SITE_ROOT.'./'.$tools_crud_position_dir.'template/'.$tools_crud_setting_tpldir.'/'.$tools_crud_delete['mod'].'/'.$tools_crud_delete['action'];
            @unlink($class_file_path);
            @unlink($module_file_path);
            @rmkdir($template_dir_path);
            //DEBUG 判断文件是否存在 删除是否成功
            if(file_exists($class_file_path)){
                showmessage($class_file_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            if(file_exists($module_file_path)){
                showmessage($module_file_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            if(is_dir($template_dir_path)){
                showmessage($template_dir_path.'删除失败,请确定是否有权限删除','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
            }
            //TODO 取出删除待删除的菜单编号 并删除菜单关系
            //DEBUG 自动删除模块功能对应的菜单
            @DB::delete('common_menu',array('cm_mod'=>$tools_crud_delete['mod'],'cm_action'=>$tools_crud_delete['action']),1);
            updatecache('setting');
            showmessage('操作成功','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
        }else{
            showmessage('非法操作','index.php?mod=tools&action=module_table&do=index&tm_id='.$tm_id);
        }
    }
    
	/*
	*  文件移动处理
	*/
    private function upload_file($src_file,$target_file){
        @copy($src_file,$target_file);
        if(!file_exists($target_file) && function_exists('move_uploaded_file'))
        {
            @move_uploaded_file($src_file,$target_file);
        }
        if(!file_exists($target_file))
        {
            if (@is_readable($src_file) && (@$fp_s = fopen($src_file, 'rb')) && (@$fp_t = fopen($target_file, 'wb'))) {
                while (!feof($fp_s)) {
                    $s = @fread($fp_s, 1024 * 512);
                    @fwrite($fp_t, $s);
                }
                fclose($fp_s); fclose($fp_t);
            }
        }
    }

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $sql_info = "SELECT tmt_id FROM ".DB::table('tools_module_table')." WHERE 1=1 ".$wheresql." ORDER BY tmt_id DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        if('json'==$return_type){
            $return = json_encode($sql_info_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($sql_info_result);
        }
		echo $return;
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>